﻿// -----------------------------------------------------------------------
// <copyright file="Calendar.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using WSU.PMP.BE.DATA.EF;

namespace WSU.PMP.BE.DATA.Concrete
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using WSU.PMP.BE.DATA.Abstract;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Calendar : ICalendar
    {
        public EfCalendar getCalendarById(string RecipeId, DateTime date)
        {
            //throw new NotImplementedException();
            using (var db = new PmpEntities())
            {
                long key = long.Parse(RecipeId);

                var calandar = (from i in db.EfCalendars
                                   where i.RecipeID == key
                                   select i).FirstOrDefault();

                return calandar;
            }

        }

        public EfCalendar getCalendarByDate(DateTime date)
        {
            //throw new NotImplementedException();
            using (var db = new PmpEntities())
            {
                var calendar = (from i in db.EfCalendars
                                 where i.Date == date
                                select i).FirstOrDefault();
                return calendar;
                              
                              }

        }

        public List<EfCalendar> getCalendarsByDateRange(DateTime start, DateTime end)
        {
            //throw new NotImplementedException();
            using (var db = new PmpEntities())
            {
               var calendar = from i in db.EfCalendars
                              where i.Date >= start && i.Date < end
                              select i;
                return calendar.ToList();
            }
        }

        public List<EfCalendar> getCalendarsByRecipe(string RecipeId)// should it be recipeId?
        {
            //throw new NotImplementedException();
            using (var db = new PmpEntities())
            {
                long key = long.Parse(RecipeId);
                var calendar = from i in db.EfCalendars
                               where i.RecipeID == key
                                   select i;
                return calendar.ToList();
            }
        }

        public void addRecipe(EfCalendar calendar)
        {
            //throw new NotImplementedException();
            using (var db = new PmpEntities())
            {
                db.EfCalendars.AddObject(calendar);
                db.SaveChanges();
            }
        }

        public void editRecipe(EfCalendar calendar)
        {
            //throw new NotImplementedException();
            using (var db = new PmpEntities())
            {
                db.EfCalendars.Attach(db.EfCalendars.Single(i=> i.RecipeID == calendar.RecipeID));
                db.EfCalendars.ApplyCurrentValues(calendar);
                db.SaveChanges();
            }
        }

        public void deleteRecipe(EfCalendar calendar)
        {
            //throw new NotImplementedException();
            using (var db = new PmpEntities())
            {
                db.EfCalendars.Attach(calendar);
                db.EfCalendars.DeleteObject(calendar);
                db.SaveChanges();
            }
        }
    }
}
